package org.ftz;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class test2 {
    public int findFile(String path) { //获取某个目录以input开头的txt文件数
        int input=0;
        final File file = new File(path);
        if (file != null){
            final File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    findFile(files[i].getAbsolutePath());
                }else{
                    if(files[i].getName().startsWith("input")){
                        input++;
                    }
                }
            }
        }
        return input;
    }
    public String txt2String(String path){ //读取文本文件内容
        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(new File(path))); //构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine()) != null) { //使用readLine方法，一次读一行
                result.append(s+System.lineSeparator());
            }
            br.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
        return result.toString().trim();
    }
    public String[] AllFileNameInput(String path){  //获取某个目录下所有TXT文件名(input)
        String[] fileName =new String[findFile(path)];
        int k=0;
        File file = new File(path);
        if (file != null){
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    findFile(files[i].getAbsolutePath());
                }else{
                    if(files[i].getName().startsWith("input")){
                        fileName[k]=files[i].getAbsolutePath();
                        k++;
                    }
                }
            }
        }
        return fileName;
    }
    public String[] AllFileNameOut(String path){  //获取某个目录下所有TXT文件名(output)
        String[] fileName =new String[findFile(path)];
        int k=0;
        File file = new File(path);
        if (file != null){
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    findFile(files[i].getAbsolutePath());
                }else{
                    if(files[i].getName().startsWith("output")){
                        fileName[k]=files[i].getAbsolutePath();
                        k++;
                    }
                }
            }
        }
        return fileName;
    }
    public String[] AllData(String path){ //获取所有input和output数据
        String data[]=new String[]{"",""};
        String[] allFileName = AllFileNameInput(path);
        String[] allFileNameOut = AllFileNameOut(path);
        for(String s:allFileName){
            String txt2String = txt2String(s);
            data[0]+=txt2String+"@";
        }
        for(String s:allFileNameOut){
            String txt2String = txt2String(s);
            data[1]+=txt2String+"@";
        }
        return data;
    }
    @Test
    public void test03(){
        String path="D:\\Google\\test\\波动数列.rar";
        String[] strings = AllFileNameInput(path);
        for(String s:strings){
            System.out.println(s);
        }
    }
    @Test
    public void test04(){
        String path="D:\\Google\\蓝桥杯\\历届试题\\地宫取宝";
        String[] strings = AllData(path);
        for(String s:strings){
            System.out.println(s);
        }
    }
    @Test
    public void test01(){
        String path="D:\\Google\\蓝桥杯\\历届试题\\波动数列";
        int file = findFile(path);
        System.out.println(file);
    }
    @Test
    public void test02(){
        String path="D:\\Google\\蓝桥杯\\历届试题\\波动数列\\input1.txt";
        String s = txt2String(path);
        System.out.println(s);
    }
}
